package best.driver.android.Model;

import best.driver.android.Beans.DriverInfo;
import best.driver.android.Beans.OrderStatus;
import best.driver.android.Utils.Logger;
import best.driver.android.manager.OrderManager;
import best.driver.android.manager.UserInfoManager;
import com.activeandroid.query.Delete;
import com.activeandroid.query.Select;

import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;


public class BestDataBaseHelper {
    private static BestDataBaseHelper dataBaseHelper = null;

    public static BestDataBaseHelper getInstance() {
        if (dataBaseHelper == null) {
            dataBaseHelper = new BestDataBaseHelper();
        }
        return dataBaseHelper;
    }

    /*
    返回地址
   */
    public  List<DriverPositionModel> getDriverPositionModels(int timeStamp) {
        List<DriverPositionModel> positionModels = new ArrayList<DriverPositionModel>();


        positionModels = new Select().from(DriverPositionModel.class)
                .where("time_stamp = ?", timeStamp)
                .limit(50)
                .execute();
        return positionModels;

    }

    /*
   返回地址
  */
    public  List<DriverPositionModel> getDriverPositionModels() {
        List<DriverPositionModel> positionModels = new Select().from(DriverPositionModel.class).limit(20)
                .execute();
        return positionModels;

    }


    public void savePosition(DriverPositionModel model) {
        List<DriverPositionModel> models = new Select()
                .from(DriverPositionModel.class)
                .where("time_stamp = ?", model.timeStamp)
                .execute();
        if(models != null && models.size() > 0) {
//            DriverPositionModel temp = models.get(0);
//            temp.save(); 重复数据，啥都不做
        } else {
            model.save();
        }
    }

    public void deletePosition(DriverPositionModel model) {
        List<DriverPositionModel> models = new Select()
                .from(DriverPositionModel.class)
                .where("time_stamp = ?", model.timeStamp)
                .execute();
        if(models != null && models.size() > 0) {
           for (int i = 0; i < models.size(); i++) {
               models.get(i).delete();
           }
        }
    }

    public void deletePosition(long timeStamp) {
        List<DriverPositionModel> models = new Select()
                .from(DriverPositionModel.class)
                .where("time_stamp = ?", timeStamp)
                .execute();
        if(models != null && models.size() > 0)
        Logger.d("MessageTrueDelete", models.size() + " " + models.toString() + " " +models.get(0).timeStamp);
        if(models != null && models.size() > 0) {
            for (int i = 0; i < models.size(); i++) {
                models.get(i).delete();
            }
        }
    }

    public String convertToString(List<DriverPositionModel> models) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(UserInfoManager.getInstance().getDriverId()).append(",");
//        if(OrderManager.getInstance().getCurrentOrder() != null) {
//            stringBuilder.append(DriverPositionModel.WORK_STATUS_BUSY);
//        } else {
//            stringBuilder.append(DriverPositionModel.WORK_STATUS_FREE);
//        }
//        DriverInfo info = UserInfoManager.getInstance().getDriverInfo();
//        stringBuilder.append(",").append(info.driverType)
//                .append(",").append(String.valueOf(info.credit)).append(",").append(info.vehicleId).append(",").append(info.cityId).append(",");
        if(models != null && models.size() > 0) {
            for(int index = 0; index < models.size(); index++) {
                DriverPositionModel model = models.get(index);
                stringBuilder.append(model.lng).append(",");
                stringBuilder.append(model.lat).append(",");
                stringBuilder.append(model.height).append(",");
                stringBuilder.append(model.direction).append(",");
                stringBuilder.append(model.timeStamp).append(",");
                stringBuilder.append(model.accuracy).append(",");
                stringBuilder.append(model.type);
                if(index < models.size() - 1) {
                    stringBuilder.append(",");
                }
            }
        }
        return stringBuilder.toString();
    }


}
